package com.sunshine.wrongquestionset.activity.question_set

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.room.entity.*
import com.sunshine.wrongquestionset.room.repository.MyRepository

/**
 * @author sunshine
 * @date 2021/3/29
 */
class QuestionSetViewModel(application: Application): AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)

    /**
     * subjectId 为 -1查询全部，大于等于0查询当前科目的
     */
    fun getQuestionSet(userId: String, subjectId: Int): LiveData<List<QuestionSetEntity>?> {
        return if (subjectId < 0) repository.getQuestionSetByUserId(userId) else repository.getQuestionSetBySubjectId(userId, subjectId)
    }

    fun getQuestionActivityBeanList(userId: String, subjectId: Int): LiveData<List<QuestionSetActivityBean>?> {
        return if (subjectId < 0) repository.getQuestionSetActivityBeanList(userId) else repository.getQuestionSetActivityBeanListBySubjectId(userId, subjectId)
    }

    fun addQuestionSet(userId: String, name: String, description: String?, subjectId: Int, backgroundColor: String) {
        repository.insertQuestionSet(userId, name, description, subjectId, backgroundColor)
    }

//    fun setDefault(userId: String, setId: Int) {
//        repository.updateAppConfigByEntity(AppConfigEntity(userId, true, setId))
//    }

    fun toDelete(questionSetBean: QuestionSetActivityBean) {
        repository.deleteQuestionSet(QuestionSetEntity(questionSetBean.id, questionSetBean.userId, questionSetBean.name, questionSetBean.description, questionSetBean.subjectId, questionSetBean.backgroundColor))
    }

    fun updateQuestionSet(questionSetBean: QuestionSetActivityBean) {
        repository.updateQuestionSet(QuestionSetEntity(questionSetBean.subjectId, questionSetBean.userId, questionSetBean.name, questionSetBean.description, questionSetBean.subjectId, questionSetBean.backgroundColor))
    }

    //2021 04 10 添加查询该用户所有科目
    fun getSubjectByUserId(userId: String): LiveData<List<SubjectEntity>?> {
        return repository.getAllSubject(userId)
    }

    fun getQuestionBySubjectId(subjectId: Int): LiveData<List<QuestionEntity>?> {
        return repository.getQuestionBySubject(subjectId)
    }

    fun getQuestionCountInQuestionSet(questionSetId: Int): LiveData<Int> {
        return repository.getQuestionCountInSetId(questionSetId)
    }

}